/*
 * @Author: 刘经标 2604132672@qq.com
 * @Date: 2025-04-12 18:00:01
 * @LastEditors: 刘经标 2604132672@qq.com
 * @LastEditTime: 2025-04-14 11:49:31
 * @FilePath: /ai-backend/src/app.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import { config } from './config/env.js';
import chatRoutes from './routes/chat.routes.js';
import { errorHandler } from './utils/apiError.js';
import { logger } from './utils/logger.js';

const app = express();

// 中间件
app.use(helmet()); // 安全头
app.use(cors({
  origin: config.NODE_ENV === 'development' ? '*' : 'https://yourdomain.com'
}));
app.use(express.json({ limit: '10kb' })); // 请求体限制

// 路由
app.use('/api', chatRoutes);

// 健康检查
app.get('/health', (req, res) => {
  res.status(200).json({ status: 'ok' });
});

// 全局错误处理
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
  errorHandler(err, req, res, next);
});

// 启动服务器
app.listen(config.PORT, () => {
  logger.info(`Server running on http://localhost:${config.PORT}`);
});